package com.fivium.bpd.component;

import java.util.Comparator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Assignment
    implements DocumentSerializable, Comparable<Assignment>
{
  public static final String ELEMENT_PROCESS_ASSIGNMENT = "PROCESS_ASSIGNMENT";
  public static final String ELEMENT_CONTEXTUAL_ASSIGNMENT = "CONTEXTUAL_ASSIGNMENT";
  public static final String ELEMENT_EXTERNAL_ASSIGNMENT = "EXTERNAL_ASSIGNMENT";
  public static final String ELEMENT_ASSIGNMENT = "ASSIGNMENT";
  public static final String ELEMENT_ASSIGNMENT_TITLE = "ASSIGNMENT_TITLE";
  public static final String ELEMENT_DEEGATION = "DELEGATION";
  
  public static enum ASSIGNMENT_TYPE {
    PROCESS, CONTEXTUAL, EXTERNAL
  };
  
  private ASSIGNMENT_TYPE assignmentType_;
  private String assignment_;
  private String assignmentTitle_;
  private String delegation_;
  
  public ASSIGNMENT_TYPE getAssignmentType()
  {
    return assignmentType_;
  }

  public void setAssignmentType(ASSIGNMENT_TYPE assignmentType)
  {
    this.assignmentType_ = assignmentType;
  }

  public String getAssignment()
  {
    return assignment_;
  }

  public void setAssignment(String assignment)
  {
    this.assignment_ = assignment;
  }

  public String getAssignmentTitle()
  {
    return assignmentTitle_;
  }

  public void setAssignmentTitle(String assignmentTitle)
  {
    this.assignmentTitle_ = assignmentTitle;
  }

  public String getDelegation()
  {
    return delegation_;
  }

  public void setDelegation(String delegation)
  {
    this.delegation_ = delegation;
  }

  @Override
  public Document serializeToDocument()
  {
    try {
      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
      Document assignmentXml = documentBuilder.newDocument();
      
      Element rootElement = null;
      switch (assignmentType_) {
        case PROCESS:
          rootElement = assignmentXml.createElement(ELEMENT_PROCESS_ASSIGNMENT);
          break;
        case CONTEXTUAL:
          rootElement = assignmentXml.createElement(ELEMENT_CONTEXTUAL_ASSIGNMENT);
          break;
        case EXTERNAL:
          rootElement = assignmentXml.createElement(ELEMENT_EXTERNAL_ASSIGNMENT);
          break;
      }
      assignmentXml.appendChild(rootElement);
      
      Element assignmentElement = assignmentXml.createElement(ELEMENT_ASSIGNMENT);
      assignmentElement.setTextContent(assignment_);
      rootElement.appendChild(assignmentElement);
      
      Element assignmentTitleElement = assignmentXml.createElement(ELEMENT_ASSIGNMENT_TITLE);
      assignmentTitleElement.setTextContent(assignmentTitle_);
      rootElement.appendChild(assignmentTitleElement);
      
      Element delegationElement = assignmentXml.createElement(ELEMENT_DEEGATION);
      delegationElement.setTextContent(delegation_);
      rootElement.appendChild(delegationElement);
      
      return assignmentXml;
    }
    catch (ParserConfigurationException e) {
      e.printStackTrace();
    }

    return null;
  }

  @Override
  public int compareTo(Assignment a)
  {
    return getAssignmentTitle().compareTo(a.getAssignmentTitle());
  }
}
